import { arrayFind, isEmpty } from 'element-ui/src/utils/util'
export function tabsBarSyle1 () {
  let style = {};
  let offset = 0;
  let tabSize = 0;
  const sizeName = ['top', 'bottom'].indexOf(this.rootTabs.tabPosition) !== -1 ? 'width' : 'height';
  const sizeDir = sizeName === 'width' ? 'x' : 'y';
  const firstUpperCase = str => {
    return str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
  };
  this.tabs.every((tab, index) => {
    let $el = arrayFind(this.$parent.$refs.tabs || [], t => t.id.replace('tab-', '') === tab.paneName);
    if (!$el) { return false; }

    if (!tab.active) {
      offset += $el[`client${firstUpperCase(sizeName)}`];
      return true;
    } else {
      tabSize = $el[`client${firstUpperCase(sizeName)}`];
      const tabStyles = window.getComputedStyle($el);
      if (sizeName === 'width' && this.tabs.length > 1) {
        tabSize -= parseFloat(tabStyles.paddingLeft) + parseFloat(tabStyles.paddingRight);
      }
      if (sizeName === 'width') {
        offset += parseFloat(tabStyles.paddingLeft);
      }
      return false;
    }
  });

  const transform = `translate${firstUpperCase(sizeDir)}(${offset}px)`;
  style[sizeName] = tabSize + 'px';
  style.transform = transform;
  style.msTransform = transform;
  style.webkitTransform = transform;

  return style;
}
export function computePresentText() {
  const { checkedValue, config } = this;
  if (!isEmpty(checkedValue)) {
    const node = this.panel.getNodeByValue(checkedValue);
    if (node === null && Array.isArray(checkedValue)) {
      this.presentText = checkedValue.join('／')
      return;
    }
    if (node && (config.checkStrictly || node.isLeaf)) {
      this.presentText = node.getText(this.showAllLevels, this.separator);
      return;
    }
  }
  this.presentText = null;
}
export function tabsBarSyle () {
  var _this = this;

  if (!this.$parent.$refs.tabs) return {};
  var style = {};
  var offset = 0;
  var tabSize = 0;
  var sizeName = ['top', 'bottom'].indexOf(this.rootTabs.tabPosition) !== -1 ? 'width' : 'height';
  var sizeDir = sizeName === 'width' ? 'x' : 'y';
  var firstUpperCase = function firstUpperCase(str) {
    return str.toLowerCase().replace(/( |^)[a-z]/g, function (L) {
      return L.toUpperCase();
    });
  };

  this.tabs.forEach(function (tab, index) {
    var $el = _this.$parent.$refs.tabs[index];
    if (!$el) {
      return
    }
    if ($el['client' + firstUpperCase(sizeName)] > 0) {
      $el.dataset['catch_client' + firstUpperCase(sizeName)] = $el['client' + firstUpperCase(sizeName)] || 0
    }
  })

  this.tabs.every(function (tab, index) {

    var $el = _this.$parent.$refs.tabs[index];
    if (!$el) {
      return false;
    }

    if (!tab.active) {
      offset += $el['client' + firstUpperCase(sizeName)] || Number($el.dataset['catch_client' + firstUpperCase(sizeName)]) || 0;
      return true;
    } else {
      tabSize = $el['client' + firstUpperCase(sizeName)] || Number($el.dataset['catch_client' + firstUpperCase(sizeName)]) || 0;
      if (sizeName === 'width' && _this.tabs.length > 1) {
        tabSize -= 16
      }
      return false;
    }
  });

  if (sizeName === 'width') {
    offset += 8;
  }
  var transform = 'translate' + firstUpperCase(sizeDir) + '(' + offset + 'px)';
  style[sizeName] = tabSize + 'px';
  style.transform = transform;
  style.msTransform = transform;
  style.webkitTransform = transform;

  return style;
}
// select组件 cacheOptions不释放修改
export function onOptionDestroy (index) {
  if (index > -1) {
    this.optionsCount--;
    this.filteredOptionsCount--;
    this.cachedOptions.splice(index, 1);
    this.options.splice(index, 1);
  }
}

//TimePicker TimeSelect DatePicker 销毁时销毁this.picker实例。（解决内存泄漏问题）
export function pickerBeforeDestroy () {
  try {
    if (this.picker) {
      this.unmountPicker()
    }
  } catch (e) {
  }
}
